package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Role;
import com.example.demo.service.impl.RoleServiceImpl;
import org.apache.commons.collections4.Get;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wzz
 * @since 2023-09-23
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleServiceImpl roleServiceImpl;

    @PostMapping("/add")
    public Result add(@RequestBody Role role){
        return Result.success(roleServiceImpl.saveUser(role));
    }


    @GetMapping("/list")
    public Result findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        IPage<Role> page = new Page<>(pageNum,pageSize);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        return Result.success(roleServiceImpl.page(page,queryWrapper));
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable String id){
        return Result.success(roleServiceImpl.getById(id));
    };

    @DeleteMapping("/del/{id}")
    public Result deleteRole(@PathVariable String id){
        return Result.success(roleServiceImpl.removeById(id));
    }

    @PostMapping("/roleMenu/{roleId}")
    public Result roleMenu(@PathVariable Integer roleId, @RequestBody List<Integer> menuIds){
        roleServiceImpl.setRoleMenu(roleId,menuIds);
        return Result.success();
    }

    @GetMapping("/roleMenu/{roleId}")
    public Result getRoleMenu(@PathVariable Integer roleId){
        return Result.success(roleServiceImpl.selectByRoleId(roleId));
    }

    @GetMapping("/role_list")
    public Result findAll(){
        return Result.success(roleServiceImpl.list());
    }
}
